#!/usr/bin/env bash
# vim:ts=4:sw=4:expandtab

############################################################
# NAME
#     mklog - a toy for output mkx's log.
#
# SYNOPSIS
#     mkdd error|normal|debug message ...
#
# AUTHORS
#     neiku project <ku7d@qq.com> 
#
# VERSION
#     2015/11/21: 简单支持日志按优化级高亮输出
#                 error  -> STDERR(红色)
#                 normal -> STDOUT(绿色)
#                 debug  -> STDOUT(黄色)
#     2016/01/23: debug日志应该输出到STDERR
#                 只有normal日志能输出到STDOUT
#     2016/01/31: 支持提示级日志(不含换行符)
#                 tip    -> STDOUT(紫色)
#     2017/01/16: 支持跨平台
#                 - mac的bash 3.2.57无彩色输出，新bash覆盖不了
#                   /bin/bash
#                 - 通过env程序+PATH变量动态查询bash，控制权
#                   、准确性都得到保障
#
############################################################

function normaltext()  { echo -n  "$*"; } 
function redtext()     { echo -en "\e[31;1m$*\e[0m"; }
function greentext()   { echo -en "\e[32;1m$*\e[0m"; }
function pinktext()    { echo -en "\e[35;1m$*\e[0m"; }
function yellowtext()  { echo -en "\e[33;1m$*\e[0m"; }

if [ $# -lt 2 ]
then
    echo "usage: $0 error|normal|debug message ..." >&2
    exit 1
fi

type="$1"
shift 1

if [ "$type" = "error" ]
then
    echo "`redtext $*`" >&2
elif [ "$type" = "normal" ]
then
    echo "`greentext $*`"
elif [ "$type" = "tip" ]
then
    echo -n "`pinktext "$*"`"
elif [ "$type" = "debug" ]
then
    if [ -n "$debug" ] ; then echo "`yellowtext "$*"`" >&2; fi
fi
